﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainFrame
{
    public partial class PageIO : Form
    {

        public PageIO()
        {
            InitializeComponent();
        }
        bool IsFirst = true;
        public List<RadioButton> lst_radios_input = new List<RadioButton>();
        public List<RadioButton> lst_radios_output = new List<RadioButton>();
        private void PageIO_Load(object sender, EventArgs e)
        {
            if(IsFirst)
            {
                foreach (var rbt in tableLayoutPanel3.Controls)
                {
                    if (rbt is RadioButton)
                        lst_radios_output.Add((RadioButton)rbt);
                }
                var arr_out = lst_radios_output.OrderBy(a => a.TabIndex).ToArray();
                var arr_card1 = Enum.GetNames(typeof(e_output_card1));
                var arr_excard1 = Enum.GetNames(typeof(e_output_extCard1));
                var arr_excard2 = Enum.GetNames(typeof(e_output_extCard2));
                for (int M = 0; M < arr_out.Length; M++)
                {
                    if (M < arr_card1.Length)
                        arr_out[M].Text = $"I1.{M} " + arr_card1[M];
                    else if (M >= 8 && M < (arr_excard1.Length + 8))
                        arr_out[M].Text = $"I2.{M - 8} " + arr_excard1[M - 8];
                    else if (M >= 24 && M < (arr_excard2.Length + 24))
                        arr_out[M].Text = $"I3.{M - 24} " + arr_excard2[M - 24];
                    else
                        arr_out[M].Text = "预留";
                }
                foreach (var rbt in tableLayoutPanel2.Controls)
                {
                    if (rbt is RadioButton)
                        lst_radios_input.Add((RadioButton)rbt);
                }
                var arr_in = lst_radios_input.OrderBy(a => a.TabIndex).ToArray();
                var arr_cardin1 = Enum.GetNames(typeof(e_input_card1));
                var arr_excardin1 = Enum.GetNames(typeof(e_input_extCard1));
                var arr_excardin2 = Enum.GetNames(typeof(e_input_extCard2));
                for (int M = 0; M < arr_in.Length; M++)
                {
                    if (M < arr_cardin1.Length)
                        arr_in[M].Text = $"I1.{M} " + arr_cardin1[M];
                    else if (M >= 8 && M < (arr_excardin1.Length + 8))
                        arr_in[M].Text = $"I2.{M - 8} " + arr_excardin1[M - 8];
                    else if (M >= 24 && M < (arr_excardin2.Length + 24))
                        arr_in[M].Text = $"I3.{M - 24}  " + arr_excardin2[M - 24];
                    else
                        arr_in[M].Text = "预留";
                }
                Task.Factory.StartNew(new Action(() =>
                {
                    try
                    {
                        while (!GlobalVar.isExit)
                        {
                            Thread.Sleep(50);
                            this.BeginInvoke(new Action(() =>
                            {
                                for (int M = 0; M < arr_out.Length; M++)
                                {
                                    if (M < GlobalMotion.Output_Card1.Length)
                                        arr_out[M].Checked = GlobalMotion.Output_Card1[M];

                                    else if (M >= 8 && M < (GlobalMotion.Output_CardEx1.Length + 8))
                                        arr_out[M].Checked = GlobalMotion.Output_CardEx1[M - 8];

                                    else if (M >= 24 && M < (GlobalMotion.Output_CardEx2.Length + 24))
                                        arr_out[M].Checked = GlobalMotion.Output_CardEx2[M - 24];
                                    else
                                        arr_out[M].Checked = false;
                                }
                            }));
                        }
                    }
                    catch (Exception ex)
                    {
                        displalyInfo.displayErrorMain(ex.Message);
                    }
                }));
                Task.Factory.StartNew(new Action(() =>
                {
                    try
                    {
                        while (!GlobalVar.isExit)
                        {
                            Thread.Sleep(50);
                            this.BeginInvoke(new Action(() =>
                            {
                                for (int M = 0; M < arr_in.Length; M++)
                                {
                                    if (M < GlobalMotion.Input_Card1.Length)
                                        arr_in[M].Checked = GlobalMotion.Input_Card1[M];

                                    else if (M >= 8 && M < (GlobalMotion.Input_CardEx1.Length + 8))
                                        arr_in[M].Checked = GlobalMotion.Input_CardEx1[M - 8];

                                    else if (M >= 24 && M < (GlobalMotion.Input_CardEx2.Length + 24))
                                        arr_in[M].Checked = GlobalMotion.Input_CardEx2[M - 24];
                                    else
                                        arr_in[M].Checked = false;
                                }
                            }));
                        }
                    }
                    catch (Exception ex)
                    {
                        displalyInfo.displayErrorMain(ex.Message);
                    }
                }));
            }
        }
        
     
    }
}
